package logic.avatar
{
	import common.avatar.AvatarSprite;
	import common.core.GSprite;
	import common.manager.keymanager.Key;
	import common.manager.keymanager.KeyCode;
	import common.manager.keymanager.KeyCombo;
	import common.world.World;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class Pand extends AvatarSprite
	{
		public static const STATU_WALK : String = "walk";
		public static const STATU_RUN : String = "run";
		public static const STATU_JUMP : String = "jump";
		public static const STATU_ATTACK : String = "attack";
		
		private var m_id : String;
		
		private var key : Key;
		
		private var keyW : KeyCombo;
		private var keyA : KeyCombo;
		private var keyS : KeyCombo;
		private var keyD : KeyCombo;
		private var keyJ : KeyCombo;
		private var keySpace : KeyCombo;
		
		private var vx : Number;
		private var vy : Number;
		
		public var attackLength : Number;
		
		private var keyBool : Object ;
		
		private var m_hand : PandHand;
		
		public function Pand()
		{
			key = new Key;
			
			curStatu = STATU_WALK;
			
			keyW = key.createKeyCombo(KeyCode.VK_W);
			keyA = key.createKeyCombo(KeyCode.VK_A);
			keyS = key.createKeyCombo(KeyCode.VK_S);
			keyD = key.createKeyCombo(KeyCode.VK_D);
			keyJ = key.createKeyCombo(KeyCode.VK_J);
			keySpace = key.createKeyCombo(KeyCode.VK_SPACE);
			
			vx = 0;
			vy = 0;
			
			attackLength = 90;
			
			keyBool = new Object;
			
			m_hand = new PandHand;
			
			World.addEntity("pandHand",m_hand);
			
			keyW.addEventListener(KeyCombo.COMBO_PRESSED,onMDW);
			keyA.addEventListener(KeyCombo.COMBO_PRESSED,onMDA);
			keyS.addEventListener(KeyCombo.COMBO_PRESSED,onMDS);
			keyD.addEventListener(KeyCombo.COMBO_PRESSED,onMDD);
			keyW.addEventListener(KeyCombo.COMBO_RELEASED,onMUW);
			keyA.addEventListener(KeyCombo.COMBO_RELEASED,onMUA);
			keyS.addEventListener(KeyCombo.COMBO_RELEASED,onMUS);
			keyD.addEventListener(KeyCombo.COMBO_RELEASED,onMUD);
			keyJ.addEventListener(KeyCombo.COMBO_PRESSED,onMDJ);
			keyJ.addEventListener(KeyCombo.COMBO_RELEASED,onMUJ);
			keySpace.addEventListener(KeyCombo.COMBO_PRESSED,onMDSpace);
			keySpace.addEventListener(KeyCombo.COMBO_RELEASED,onMUSpace);
		}
		
		private function detory() : void
		{
			key.removeKeyCombo(keyW);
			key.removeKeyCombo(keyA);
			key.removeKeyCombo(keyS);
			key.removeKeyCombo(keyD);
			key.removeKeyCombo(keyJ);
			key.removeKeyCombo(keySpace);
			
			keyW.removeEventListener(KeyCombo.COMBO_PRESSED,onMDW);
			keyA.removeEventListener(KeyCombo.COMBO_PRESSED,onMDA);
			keyS.removeEventListener(KeyCombo.COMBO_PRESSED,onMDS);
			keyD.removeEventListener(KeyCombo.COMBO_PRESSED,onMDD);
			keyW.removeEventListener(KeyCombo.COMBO_RELEASED,onMUW);
			keyA.removeEventListener(KeyCombo.COMBO_RELEASED,onMUA);
			keyS.removeEventListener(KeyCombo.COMBO_RELEASED,onMUS);
			keyD.removeEventListener(KeyCombo.COMBO_RELEASED,onMUD);
			keyJ.removeEventListener(KeyCombo.COMBO_RELEASED,onMUJ);
			keyJ.removeEventListener(KeyCombo.COMBO_RELEASED,onMUJ);
			keySpace.removeEventListener(KeyCombo.COMBO_PRESSED,onMDSpace);
			keySpace.removeEventListener(KeyCombo.COMBO_RELEASED,onMUSpace);
			
			key = null;
			keyW = null;
			keyA = null;
			keyS = null;
			keyD = null;
			keySpace = null;
			keyBool = false;
		}
		
		private function onMDW(evt:Event) : void
		{
			vy = -5;
			keyBool["w"] = true;
			checkStatu();
		}
		
		private function onMDA(evt:Event) : void
		{
			vx = -5;
			keyBool["a"] = true;
			this.scaleX = -1;
			checkStatu();
		}
		
		private function onMDS(evt:Event) : void
		{
			vy = 5;
			keyBool["s"] = true
			checkStatu();
		}
		
		private function onMDD(evt:Event) : void
		{
			vx = 5;
			keyBool["d"] = true;
			this.scaleX = 1;
			checkStatu();
		}
		
		private function onMUW(evt:Event) : void
		{
			keyBool["w"] = false;
			checkStatu();
		}
		
		private function onMUA(evt:Event) : void
		{
			keyBool["a"] = false;
			checkStatu();
		}
		
		private function onMUS(evt:Event) : void
		{
			keyBool["s"] = false;
			checkStatu();
		}
		
		private function onMUD(evt:Event) : void
		{
			keyBool["d"] = false;
			checkStatu();
		}
		
		private function onMDSpace(evt : Event) : void
		{
			keyBool["space"] = true;
			
			checkStatu();
		}
		
		private function onMUSpace(evt : Event) : void
		{
			keyBool["space"] = false;
		}
		
		private function onMDJ(evt : Event) : void
		{
			keyBool["j"] = true;
			
			checkStatu();
		}
		
		private function onMUJ(evt :Event) : void
		{
			keyBool["j"] = false;
			
			checkStatu();
		}
		
		private function checkStatu():void
		{
			if(keyBool["space"])
			{
				if(curStatu != STATU_JUMP)
				{
				  curStatu = STATU_JUMP;
				}
			}
			else
			{
				if(keyBool["w"] || keyBool["a"] || keyBool["s"] || keyBool["d"])
				{
					curStatu = STATU_WALK;
				}
				
				if(keyBool["j"])
				{
					curStatu = STATU_ATTACK;
				}
			}
		}
		
		public override function collision(object:GSprite):void
		{
			
		}
		
		public override function unCollision(object:GSprite):void
		{
			
		}
		
		public override function update():void
		{
			if(keyBool["a"] || keyBool["d"])
			{
				this.x += vx;
			}
			if(keyBool["w"] || keyBool["s"])
			{
				this.y += vy;
			}
			
			if(body)
			if(body["pand"])
			{
				if(scaleX > 0)
				{
					m_hand.x = x+body["pand"]["Hand"].x;
					m_hand.y = y+body["pand"]["Hand"].y;
				}
				else
				{
					m_hand.x = x-body["pand"]["Hand"].x;
					m_hand.y = y+body["pand"]["Hand"].y;
				}
			}
			super.update();
		}
	}
}